package com.example.activity_service.dao;

import com.example.activity_service.vo.Commodity;
import com.example.activity_service.vo.StopWatch;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;

@Repository
@Mapper
public interface Msdao {
    //    and order by msid ASC
    @Select("select * from stopwatch WHERE statu=1 order by endtime ASC  LIMIT 0,5  ")
    List<StopWatch> huoqusj();

    //查一条时间
    @Select("select * from stopwatch WHERE statu=1 order by endtime ASC  LIMIT 0,1  ")
    List<StopWatch> huoqusj1();


    //用函数
    @Insert("call cuncu(#{vava},#{commodityid},#{goodsid});")
    int tjsjzjb(StopWatch stopWatch);

    @Update("update stopwatch set statu=0 where endtime =#{endtime}")
    int gai(StopWatch stopWatch);

    //查询切换商品

    @Select("select DISTINCT o.commodity_id,c.category_id,c.id,c.commodity_price,c.commodity_picture,e.spikeprice,c.commodity_description from stopwatch a,s_t b,t_commodity c,miaosha e ,t_commodity_allocation o where a.msid=#{msid} and a.commodityid=#{commodityid} and  a.commodityid=b.commodityid and b.goodsid=c.id and  e.id=b.commodityid  and e.goodsid=b.goodsid and b.goodsid=o.commodity_id and b.state='1' ")

    List<StopWatch>qiehuan(StopWatch stopWatch);

    //首页查4个商品
    @Select("select DISTINCT o.commodity_id,c.commodity_price,c.commodity_picture,e.spikeprice,c.commodity_description from stopwatch a,s_t b,t_commodity c,miaosha e ,t_commodity_allocation o where a.msid=#{msid} and a.commodityid=#{commodityid} and  a.commodityid=b.commodityid and b.goodsid=c.id and  e.id=b.commodityid  and e.goodsid=b.goodsid and b.goodsid=o.commodity_id and b.state='1' limit 0,4 ")
    List<StopWatch>shouye(StopWatch stopWatch);


    //    查秒杀表里的时间
    @Select("select * from stopwatch WHERE msid=#{msid} ")
    List<StopWatch> msid(StopWatch stopWatch);

    @Update("update t_commodity set seckill=seckill-1 where goodsid=#{goodis}")
    int jianyi(int goodsid);

    //数量变成0 中间表
//    @Update("update s_t set state=0 where goodsid=(select goodsid from t_commodity where seckill=0)")
//    int shuliang0();

    //查商品
    @Select("select * from t_commodity WHERE status=1 ")
    List<StopWatch> chagoods();

    //添加时间表
    @Insert("insert into stopwatch (commodityid,openingtime,endtime,creationtime,statu) values(#{commodityid},#{openingtime},#{endtime},now(),1)")
    int tjsj(StopWatch stopWatch);


    //添加商品时间表
    @Insert("insert into miaosha (id,goodsid,spikeprice,miao_time,miao_time2)values((select commodityid from  stopwatch where openingtime =#{openingtime}),#{goodsid},#{spikeprice},#{openingtime},(select endtime from stopwatch where openingtime=#{openingtime}))")
    int shangpintianjia(String openingtime, String spikeprice, String goodsid);

    //添加商品关联表
    @Insert("insert into s_t (commodityid,goodsid,state)values((select commodityid from  stopwatch where openingtime =#{openingtime}),#{goodsid},1)")
    int miaoshaguanliang(String openingtime, String goodsid);


}
